<script setup>
import { ref } from "vue";
import { convertFileSrc } from '@tauri-apps/api/core';
import { useNotifyStateStore } from "@/stores/notifyState";

const notifyStore = useNotifyStateStore();

const plugin_src = ref("");
const plugin_apps = ref([]);

function eval_js() {
    const test_url = convertFileSrc("D:\\Code\\luckyplayer\\tmp\\t1.js");
    console.log("url", test_url);

    import(test_url).then(res => {
        var pluginInfo = res.default();
        console.log(pluginInfo);
        pluginInfo.onLoad();
        // pluginInfo.onUnload();
        var dt = pluginInfo.getAudioList();
        plugin_apps.value.push({
            ...pluginInfo,
            data: dt,
            path: test_url,
        });
    })
    // var plugin_rslt = eval(test_url);
    // console.log(plugin_rslt);
}

function play_to(item) {
    var inf = {
        path: item.path ?? '',
        title: item.title ?? '',
        artist: item.artist ?? '',
        album: item.album ?? '',
        genre: item.genre ?? '',
        album_artist: item.album_artist ?? '',
        duration: item.duration ?? 0,
        bitrate: item.bitrate ?? 0,
        sample_rate: item.sample_rate ?? 0,
        bit_depth: item.bit_depth ?? 0,
        channels: item.channels ?? 1,
        year: item.year ?? 0,
        track: item.track ?? 0,
        comment: item.comment ?? '',
        size: item.size ?? 0,
        lyric: item.lyric ?? '',
        cover: item.cover ?? '',
        dir: item.dir ?? '',

        is_online: true,
        method: item.method ?? 'GET',
        headers: item.headers ?? {},
    };
    // console.log(inf);
    notifyStore.player_ctrl = {
        cmd: "play_to",
        val: inf,
    };
}

</script>
<template>
    <n-h1>音乐工具</n-h1>
    <n-p>匹配源设定：<span>网易云</span></n-p>
    <n-space>
        <n-button type="primary" @click="">批量自动匹配歌词</n-button>
        <n-button type="primary" @click="">批量自动匹配封面</n-button>
        <n-button type="primary" @click="">批量自动匹配歌曲信息(仅使用曲名和歌手匹配，其他信息自动填充)</n-button>
    </n-space>
    <n-h1>插件管理</n-h1>
    <n-space vertical>
        <n-input type="text" placeholder="插件Url地址" v-model:value="plugin_src" />
        <n-button type="primary" @click="eval_js">添加插件</n-button>
        <ul>
            <li v-for="item in plugin_apps" :key="item.name">
                <b>{{ item.name + ' => ' + item.path }}</b>
                <div v-for="(dt, i) in item.data" :key="i" @click="play_to(dt)" :title="dt.path"
                    class="p-1 m-1 bg-yellow-200 cursor-pointer">{{ dt.title + '/' + dt.artist }}</div>
            </li>
        </ul>
    </n-space>
</template>

<style scoped></style>
